package com.park.web.controller.admin;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.park.client.model.user.MenuModel;
import com.park.client.model.user.PermissionModel;
import com.park.client.service.user.MenuService;
import com.park.client.service.user.PermissionService;
import com.park.common.constant.Constant;
import com.park.common.util.Page;
import com.park.common.util.PageData;
import com.park.common.util.Result;
import com.park.web.contant.Global;
import com.park.web.controller.BaseController;

@Controller
@RequestMapping("/admin/permission")
public class AdminPermissionController extends BaseController{

	@Autowired
	private PermissionService permissionService;
	@Autowired
	private MenuService menuService;
	
	@RequestMapping("listRows")
	@ResponseBody
	public PageData<PermissionModel> listRows(Integer rows,@RequestParam(name="page",defaultValue="1")Integer pageNow,HttpServletRequest request){
		PageData<PermissionModel>  pageData = new PageData<>();
		PermissionModel permission = new PermissionModel();
		Integer total = permissionService.selectTotal(permission);
		Page page = new Page(pageNow,total,rows);
		permission.setStartPos(page.getStartPos());
		permission.setPageSize(page.getPageSize());
		List<PermissionModel> list = permissionService.selectPagination(permission);
		pageData.setTotal(total);
		pageData.setRows(list);
		return pageData;
	}
	
	@RequestMapping("permissionList")
	public String permissionList(HttpServletRequest request){
			return Global.ADMIN_PATH+"permission/permissionList";
	}
	
	/**
	 * 添加按钮
	 * @return
	 */
	@RequestMapping("save")
	@ResponseBody
	public Result<String> save(PermissionModel permission,HttpServletRequest request){
		Result<String> json = new Result<>(true);
		/**
		 * 添加之前校验权限码是否已经存在
		 */
		boolean success = checkHasExists(permission);
		if(success){
			success = permissionService.insertSelective(permission);
			if(success){
				MenuModel record =new MenuModel();
				record.setCategory(1);
				record.setText(permission.getButtonName());
				record.setPid(permission.getMenuId());
				record.setPermissionCode(permission.getPermissionCode());
				Integer maxSort = menuService.selectMaxSort(permission.getMenuId());
				record.setSort(maxSort != null?maxSort+1:1);
				menuService.insertSelective(record);
				menuService.updateState(permission.getMenuId());//改状态
				json.setMessage(Constant.SUCCESS_SAVE_MSG);
			}else{
				json.setSuccess(false);
				json.setMessage(Constant.FAIL_SAVE_MSG);
			}
		}else{
			json.setSuccess(false);
			json.setMessage(Constant.FAIL_PERMISSION_CODE);
		}
		return json;
	}

	private boolean checkHasExists(PermissionModel permission) {
		if(permission != null){
			PermissionModel permission2 = new PermissionModel();
			permission2.setPermissionCode(permission.getPermissionCode());
			return  permissionService.selectByModel(permission2) ==null;
		}
		return false;
	}
	
	/**
	 * 删除按钮
	 * @return
	 */
	@RequestMapping("deleteById")
	@ResponseBody
	public Result<String> deleteById(PermissionModel permission,HttpServletRequest request){
		Result<String> json = new Result<>(true);
		boolean success = permissionService.deleteByPrimaryKey(permission.getId());
		if(success){
			json.setMessage(Constant.SUCCESS_DELETE_MSG);
			MenuModel menu = new MenuModel();
			menu.setPermissionCode(permission.getPermissionCode());
			menuService.deleteByModel(menu);
		}else{
			json.setMessage(Constant.FAIL_DELETE_MSG);
			json.setSuccess(false);
		}
		return json;
	}
	
	/**
	 * 修改按钮
	 * @return
	 */
	@RequestMapping("edit")
	@ResponseBody
	public Result<String> edit(PermissionModel permission,HttpServletRequest request){
		Result<String> json = new Result<>(true);
		
		boolean success = permissionService.updateByPrimaryKeySelective(permission);
		if(success){
			json.setMessage(Constant.SUCCESS_UPDATE_MSG);
			json.setSuccess(true);
		}else{
			json.setMessage(Constant.FAIL_UPDATE_MSG);
			json.setSuccess(false);
		}
		return json;
	}
}
